//
//  STChannelFooterView.swift
//  StarTimesON
//
//  Created by Toj on 9/26/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

protocol STChannelFooterViewDelegate: NSObjectProtocol {
    func channelFooterView(_ view: STChannelFooterView, didTapControl button: UIButton)
}

class STChannelFooterView: UICollectionReusableView {
    
    weak var delegate: STChannelFooterViewDelegate?
    
    var mobileViewModel: MBCommodityChannelsModel? { didSet {
        guard let model = mobileViewModel else { return }
        controlButton.isSelected = model.isOpened
        let channelsCount = model.channels.count
        shadowView.isHidden = channelsCount == 0
        controlButton.isHidden = channelsCount <= 1
        ds_paymentShow()
    } }
    
    var tvViewModel: TVChannelsViewModel? { didSet {
        guard let model = tvViewModel else { return }
        controlButton.isSelected = model.isOpened
        let channelsCount = model.channels.count
        shadowView.isHidden = channelsCount == 0
        controlButton.isHidden = channelsCount <= 1
        ds_paymentShow()
    } }
    
    @objc private func didTapControl(sender: UIButton) {
        guard let model = mobileViewModel else {
            guard let model = tvViewModel else { return }
            model.isOpened = !model.isOpened
            sender.isSelected = model.isOpened
            delegate?.channelFooterView(self, didTapControl: sender)
            return
        }
        model.isOpened = !model.isOpened
        sender.isSelected = model.isOpened
        delegate?.channelFooterView(self, didTapControl: sender)
    }
    
    private lazy var shadowContentView: UIView = {
        let view = UIView()
        view.clipsToBounds = true
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(shadowContentView)
        shadowContentView.addSubview(shadowView)
        addSubview(controlButton)
        addSubview(borderLineView)
        
        clipsToBounds = true
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        borderLineView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(self)
            make.height.equalTo(8.ui_scale)
        }
        
        shadowContentView.snp.makeConstraints { make in
            make.bottom.equalTo(borderLineView.snp.top).offset(-10.ui_scale)
            make.left.right.equalTo(self).inset(15)
            make.height.equalTo(46.ui_scale)
        }
        shadowView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(shadowContentView)
            make.height.equalTo(46.ui_scale)
        }
        
        controlButton.snp.makeConstraints { make in
            make.top.equalTo(shadowView)
            make.left.right.equalTo(shadowView).inset(4)
            make.height.equalTo(40.ui_scale)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var shadowView: UIImageView = {
        let view = UIImageView()
        view.image = "commodity_channel_shadow_bottom".to_image
        return view
    }()
    
    private lazy var controlButton: UIButton = {
        let view = STButton()
        view.disableHighlighted = true
        view.backgroundColor = .white
        view.setTitleColor("#666666".to_color, for: .normal)
        view.titleLabel?.font = UIFont.systemFont(ofSize: 10.f_scale)
        // 展开/ 收起按钮的图标
        view.setImage("commodity_channel_stretch".to_image, for: .normal)
        view.setImage("commodity_channel_folding".to_image, for: .selected)
        
        view.st_cornerRadius = 8
        view.addTarget(self, action: #selector(didTapControl(sender:)), for: .touchUpInside)
        view.titleAlignment = .left
        view.spacing = 4.ui_scale
        return view
    }()
    
    private lazy var borderLineView: UIView = {
        let view = UIView()
        view.backgroundColor = "#EEEEEE".to_color
        return view
    }()
}

extension STChannelFooterView {
    // 收起展开报数
    func ds_paymentShow() {
        let label = controlButton.isSelected ? "1" : "0"
        let event = DSEvent(category: DSVCategory.membershipList.rawValue, action: DSVAction.OTTMembership.paymentShow.rawValue,
                            label: label)
        DataStatistics.sendEvent(event: event)
    }
}
